package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/9/5 09:53
 * @apiNote
 */

public class Test01360DaysBetweenDates {
    public int daysBetweenDates(String date1, String date2) {
        return Math.abs(dateToInt(date2) - dateToInt(date1));
    }

    public int dateToInt(String date){
        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);
        int day = Integer.parseInt(split[2]);
        int[] monthLength = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int res = day-1;
        while(month != 0){
            month--;
            res += monthLength[month];
            if(month == 2 && isLeapYear(year)){
                res++;
            }
        }
        res += 365 *(year - 1971);
        res +=(year -1)/4 -1971/4;
        res -=(year -1)/100 -1971/100;
        res +=(year - 1)/400 -1971/400;
        return res;
    }

    public boolean isLeapYear(int year){
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
}
